package com.ql.controller;

import com.ql.service.socket.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

//@Controller
public class InitController extends HttpServlet {
    private final int sleepTime = 5 * 60 * 1000;
    @Autowired
    Server server;

    @Override
    public void init() throws ServletException {
        FutureTask<String> task = new FutureTask<String>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                taskstart(); //使用另一个线程来执行该方法，会避免占用Tomcat的启动时间
                return "Collection Completed";
            }
        });

        new Thread(task).start();
    }

    private void taskstart() {
        while (true) {
            try {
                System.out.println("init");
                System.out.println("==============================");
                server.start();
                System.out.println("==============================");

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}
